package com.lzx.adx.report.support.strategy.v3.channel;

import cn.hutool.http.HttpUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.OcpxAccountDTO;
import com.lzx.adx.report.dto.ProductConfigDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.support.strategy.v2.channel.AbstractReport;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@ProductType(ProductChannelConstant.HYXT)
public class HyxtReport extends AbstractProductReport {
    @Override
    public String execute(ReportDTO reportDTO, OcpxAccountDTO ocpxAccountDTO, ProductConfigDTO productConfigDTO) {

        Map<String, Object> params = new HashMap<>();
        params.put("androidid", reportDTO.getHash_android_id());
        params.put("clickid", reportDTO.getBizRequestId());
        params.put("imei", StringUtils.trim(reportDTO.getImeiSum()));
        params.put("imei_md5", StringUtils.trim(reportDTO.getImeiSum()));
        params.put("oaid", StringUtils.trim(reportDTO.getOaid()));
        params.put("oaid_md5", StringUtils.trim(reportDTO.getOaidSum()));
        params.put("ip", StringUtils.trim(reportDTO.getIp()));

        String callbackUrl = String.format(productConfigDTO.getCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = UrlUtil.covertParamsOrderByAsc(params) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        String finalUrl = ocpxAccountDTO.getReportUrl() + "&" + urlParams;

        log.info("账号为{},请求产品端的完整地址为 {}", ocpxAccountDTO.getId(), finalUrl);
        String body = HttpUtil.createGet(finalUrl).execute().body();
        log.info("账号为{} 请求产品端，响应的结果为{}", ocpxAccountDTO.getId(), body);

        ocpxAccountDTO.setFinalURL(finalUrl);
        return body;
    }

}
